<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>jqqqqq</title>
    <link rel="stylesheet" href="lib/css/bootstrap.min.css" />
  </head>
  <body>
    <div>
      <table class="table table-bordered table-hover" id="table">
        <tr>
          <td>编号</td>
          <td>名称</td>
        </tr>
      </table>

      编号：<input type="text" id="addDataId" /> 名称:
      <input type="text" id="addDataName" />
      <span class="btn btn-primary" id="btnAdd">添加</span>
    </div>

    <script src="lib/js/jquery.min.js"></script>
    <script src="lib/js/bootstrap.min.js"></script>
    <script>
      $(function() {
        var list = [
          { id: 100, name: '你的妈妈没有了' },
          { id: 101, name: '你的爸爸没有了' }
        ];

        $.each(list, function(i, v) {
          var table = $('#table');
          var tr = $(document.createElement('tr'));
          var td = $(document.createElement('td'));
          td.append(v.id);
          tr.append(td);
          td = $(document.createElement('td'));
          td.append(v.name);
          tr.append(td);
          table.append(tr);
        });

        $('#btnAdd').click(function() {
          var data = {
            id: $('#addDataId').val(),
            name: $('#addDataName').val()
          };
          list.push(data);
          console.log(data);

          var table = $('#table');
          var tr = $(document.createElement('tr'));
          var td = $(document.createElement('td'));
          td.append(data.id);
          tr.append(td);
          td = $(document.createElement('td'));
          td.append(data.name);
          tr.append(td);
          table.append(tr);
          $('#addDataId').val('');
          $('#addDataName').val('');
        });

        
      });
    </script>
  </body>
</html>
